package com.wonders.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 〈自定义redis序列化方式〉中文的方式显示{
 "studentId": 1,
 "studentName": "小明",
 "age": 20,
 "phone": "13466668888",
 "addr": "上海市闵行区"
 }
 * @author yangyalin
 * @create 2018/11/1
 * @since 1.0.0
 */
@Configuration //相当于XML中的beans
public class RedisConfig {
    /**
     * @Author yangyalin
     * @Description redisTemplate序列化使用的jdkSerializeable, 存储二进制字节码（默认）,
     * 所以自定义序列化类,用于存储可视化内容
     * @Date 15:07 2018/11/1
     * @Param [redisConnectionFactory]
     * @return org.springframework.data.redis.core.RedisTemplate<java.lang.Object,java.lang.Object>
     **/
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //使用jackson2JsonRedisSerializer替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer
                =new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //这个一旦注释掉就会报异常信息
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        //设置key和value的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
//    /**
//     * @Author yangyalin
//     * @Description 注入消息监听容器
//     * redis消息监听器容器
//     * 可以添加多个监听不同话题的redis监听器，只需要把消息监听器和相应的消息订阅处理器绑定，该消息监听器
//     * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
//     * @Date 11:33 2018/11/22
//     * @Param [redisConnectionFactory, messageListenerAdapter1:监听处理器1, messageListenerAdapter2]
//     * messageListenerAdapter1参数名称需和监听处理器的方法名称一致，因为@Bean注解默认注入的id就是方法名称
//     * @return org.springframework.data.redis.listener.RedisMessageListenerContainer
//     **/
//    @Bean
//    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory,
//                                                   MessageListenerAdapter messageListenerAdapter1,
//                                                   MessageListenerAdapter messageListenerAdapter2) {
//        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
//        container.setConnectionFactory(redisConnectionFactory);
//        //订阅一个叫mq_01和mq_02的信道 可以添加多个
//        List<PatternTopic> list=new ArrayList<PatternTopic>();
//        list.add(new PatternTopic("mq_01"));
//        list.add(new PatternTopic("mq_02"));
//        //接收多个频道发送的消息
//        container.addMessageListener(messageListenerAdapter1,list);
//        //模糊匹配多个频道
//        container.addMessageListener(messageListenerAdapter2,new PatternTopic("mq_0*"));
//        return container;
//    }
//    /**
//     * @Author yangyalin
//     * @Description 消息监听处理器1
//     * @Date 14:12 2018/11/22
//     * @Param [messageReceiver：处理器类]
//     * @return org.springframework.data.redis.listener.adapter.MessageListenerAdapter
//     **/
//    @Bean
//    public MessageListenerAdapter messageListenerAdapter1(MessageReceiver messageReceiver){
//        //给messageListenerAdapter 传入一个消息接收的处理器，利用反射的方法调用“receiveMessage1”
//        //receiveMessage1:接收消息的方法名称
//        return new MessageListenerAdapter(messageReceiver,"receiveMessage1");
//    }
//    /**
//     * @Author yangyalin
//     * @Description 消息监听处理器2
//     * @Date 14:12 2018/11/22
//     * @Param [messageReceiver：处理器类]
//     * @return org.springframework.data.redis.listener.adapter.MessageListenerAdapter
//     **/
//    @Bean
//    public MessageListenerAdapter messageListenerAdapter2(MessageReceiver messageReceiver){
//        return new MessageListenerAdapter(messageReceiver,"receiveMessage2");
//    }

}
